<template>
<view class="showGoods">
			<view class="title">
				宁夏省的大学
			</view>
			<view class="list">
				<view class="item" v-for="(item, index) in trafficList" :key="index">
					<view class="img">
						<image :src="item.img" mode="aspectFill"></image>
					</view>
					<view class="content">
						<view class="name">{{ item.imgname }}</view>
					</view>
				</view>
			</view>
		</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	
	let bannerGoodsList = ref([])
	let key = ref("")
	
	// 新增trafficList数据
	const trafficList = ref([])
	
	onLoad(() => {
		getBanner()
		getCateOneLst()
		getselectedLst()
		getTrafficData()
	})
	
	function doSearch() {
		uni.showToast({
			// title:"开始搜索",
			title: key.value,
		})
	}
	
	async function getBanner() {
		let res = await uni.$post("/goodsPromoteSelect", {
			"is_selected": 0,
			"is_promote": 1,
			"page": 1,
			"number": 5
		}).then((res) => {
			console.log(res)
			if (res.code == 200) {
				// 将商品列表中的图片信息提取
				bannerGoodsList.value = res.goodsList.map(item => uni.$base_url + item.goods_big_logo)
			} else {
				uni.showToast({
					icon: "error",
					// title: '服务器错误，请稍后重试'
				})
			}
			// console.log(bannerGoodsList)
		}).catch((err) => {
			uni.showToast({
				icon: "error",
				// title: '网络请求错误'
			})	
		})
	}
	
	//商品分类列表
	const cateOneLst = ref([])
	async function getCateOneLst() {
		let res = await uni.$post("/categoryListLevel", {
			"level": 0
		})
		//判断返回数据成功
		if (res.code == 200) {
			// 对图片地址添加服务器地址
			cateOneLst.value = res.result.map(item => {
				item.icon = uni.$base_url + item.icon
				return item
			})
			console.log(cateOneLst.value);
		}
	}
	
	const selectedLst = ref([])
	async function getselectedLst() {
		
		let res = await uni.$post("/goodsPromoteSelect", {
			"is_selected": 0,
			"is_promote": 1,
			"page": 1,
			"number": 20
		})
		if (res.code == 200) {
			selectedLst.value = res.goodsList.map(item => {
				item.goods_big_logo = uni.$base_url + item.goods_big_logo
				return item
			})
		}
		console.log(selectedLst.value);
	}
	
	// 新增获取交通地图数据的方法
	async function getTrafficData() {
		try {
			const response = await uni.request({
				url: 'https://smzoycm4.lc-cn-n1-shared.com/1.1/classes/DaXue',
				method: 'GET',
				header: {
					'X-LC-Id': 'SmZOYcM4LNO7YdHkQPsVNj3s-gzGzoHsz',
					'X-LC-Key': 'h0fwbXM9QnmZb0ppHlzLVs1X',
				}
			});
			
			if (response.statusCode === 200) {
				trafficList.value = response.data.results;
			} else {
				uni.showToast({
					icon: "error",
					title: ''
				});
			}
		} catch (error) {
			uni.showToast({
				icon: "error",
				title: ''
			});
		}
	}
	
	function toDetails(id)
		{
			uni.navigateTo({
				url:`/pages/goodsDetails/goodsDetails?id=${id}`
			})
		}
</script>

<style scoped>
	/* 商品分类 */
	.cateList {
		margin-top: 40rpx;
		display: flex;
		justify-content: space-around;
	}
	
	.cateList>.item1>.log {
		width: 60rpx;
		height: 60rpx;
	}
	
	.cateList>.item1>.log>image {
		width: 100%;
		height: 100%;
	}
	
	.cateList>.item1>.name1 {
		font-size: 29rpx;
	}
	
	/*精选商品*/
	.showGoods {
		padding: 20rpx;
	}
	
	.title {
		font-size: 32rpx;
		color: #ff6700;
		margin-bottom: 20rpx;
		padding-left: 20rpx;
	}
	
	.list {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
	}
	
	.item {
		width: 48%;
		background: #fff;
		border-radius: 12rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
		overflow: hidden;
	}
	
	.img {
		width: 100%;
		height: 200rpx;
		overflow: hidden;
	}
	
	.img image {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}
	
	.content {
		padding: 16rpx;
	}
	
	.name {
		font-size: 28rpx;
		color: #333;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		min-height: 80rpx;
	}
	
	.info {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 10rpx;
	}
	
	.info text:first-child {
		color: red;
		font-size: 30rpx;
		font-weight: bold;
	}
	
	.tag {
		background: #ff6700;
		color: #fff;
		font-size: 24rpx;
		height: 37rpx;
		width: 60rpx;
		text-align: center;
	}
</style>